package com.glueball.crm.module.client.ui;

import org.fusesource.restygwt.client.JsonEncoderDecoder;

import com.glueball.crm.module.client.model.Contact;
import com.glueball.crm.module.client.model.EmailAddress;
import com.glueball.crm.module.client.model.PhoneNumber;
import com.glueball.crm.module.common.ui.ControllerWidget;
import com.glueball.crm.module.common.ui.CrmPanelWidget;
import com.glueball.crm.module.common.ui.EditableListWidget;
import com.glueball.crm.module.common.ui.EditableWidget;
import com.glueball.crm.module.common.ui.EmailAddressWidget;
import com.glueball.crm.module.common.ui.EmailAddressWidgetFactory;
import com.glueball.crm.module.common.ui.ListWidget;
import com.glueball.crm.module.common.ui.PhoneNumberWidget;
import com.glueball.crm.module.common.ui.PhoneNumberWidgetFactory;
import com.glueball.crm.module.util.CrmRequest;
import com.glueball.crm.module.util.CrmRequestCallback;
import com.glueball.crm.module.util.URLHelper;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public final class ContactWidget extends EditableWidget<Contact> {

	@UiField SimplePanel emailContainer;
	@UiField SimplePanel phoneContainer;

	private static ContactWidgetUiBinder uiBinder = GWT.create(ContactWidgetUiBinder.class);

	interface ContactWidgetUiBinder extends UiBinder<Widget, ContactWidget> {
	}

	interface ContactCodec extends JsonEncoderDecoder<Contact> {
	}

	private final ContactCodec codec = GWT.create(ContactCodec.class);

	private final CrmRequest<Contact> req = new CrmRequest<Contact>(codec);
	
	protected final String saveUrl;

	public ContactWidget(final Contact contact,
						 final SimplePanel showPanel,
						 final HorizontalPanel controllerPanel,
						 final StartView startView,
						 final String saveUrl) {
		super(showPanel,
			  controllerPanel,
			  new ShowContactWidget(contact),
			  new EditContactWidget(contact)
				);
		setEntity(contact);
		this.widget = uiBinder.createAndBindUi(this);
		this.saveUrl = saveUrl;
		addDefaultEditController();
		addDefaultSaveController();
		if (!this.add) {
			this.addDefaultCancelController();
		}
		setContainersVisible();
		//initEmails();
		//initPhones();
		start(startView);
	}

	@Override
	public void save() {
		req.save(this.saveUrl, this.editWidget.getEntity(), new CrmRequestCallback<Contact>() {
			public void onSuccess(final Contact _entity) {
				setEntity(_entity);
			}
		});
		initEmails();
		initPhones();
		setContainersVisible();
		this.add = false;
		changeView(this.showWidget);
	}


	@SuppressWarnings("unchecked")
	private void initEmails() {
		Window.alert("XX1");
		this.emailContainer.clear();
		Window.alert("XX2");
		final EmailAddressWidgetFactory emailAddressWidgetFactory =
				new EmailAddressWidgetFactory(URLHelper.SAVE_EMAIL+this.entity.getId());
		Window.alert("XX3");
		final ListWidget<EmailAddress> emailAddressList =
				new EditableListWidget<EmailAddressWidget, EmailAddress>(
								this.entity.getEmails(),
								emailAddressWidgetFactory);
		Window.alert("XX4");
		final CrmPanelWidget emailAddressBox = new CrmPanelWidget(emailAddressList, "200px", "200px");
		Window.alert("XX5");
		emailAddressBox.addController(new ControllerWidget<EmailAddressWidget>(URLHelper.getImageUrl("Refresh.png"), "Refresh",
				((EditableListWidget<EmailAddressWidget, EmailAddress>)emailAddressList).getRefreshCallback()));
		Window.alert("XX6");
		emailAddressBox.addController(new ControllerWidget<EmailAddressWidget>(URLHelper.getImageUrl("Add.png"), "Add",
				((EditableListWidget<EmailAddressWidget, EmailAddress>)emailAddressList).getAddCallback()));
		Window.alert("XX7");
		this.emailContainer.add(emailAddressBox.asWidget());
		Window.alert("XX8");
	}

	@SuppressWarnings("unchecked")
	private void initPhones() {
		this.phoneContainer.clear();
		final PhoneNumberWidgetFactory phoneNumberWidgetFactory = new PhoneNumberWidgetFactory(URLHelper.SAVE_PHONE+this.entity.getId());
		final ListWidget<PhoneNumber> phoneNumberList =
				new EditableListWidget<PhoneNumberWidget,PhoneNumber>(
								this.entity.getPhones(),
								phoneNumberWidgetFactory);
		final CrmPanelWidget phoneNumberBox = new CrmPanelWidget(phoneNumberList, "200px", "200px");
		phoneNumberBox.addController(new ControllerWidget<PhoneNumberWidget>(URLHelper.getImageUrl("Refresh.png"), "Refresh",
				((EditableListWidget<PhoneNumberWidget,PhoneNumber>)phoneNumberList).getRefreshCallback()));
		phoneNumberBox.addController(new ControllerWidget<PhoneNumberWidget>(URLHelper.getImageUrl("Add.png"), "Add",
				((EditableListWidget<PhoneNumberWidget,PhoneNumber>)phoneNumberList).getAddCallback()));
		this.phoneContainer.add(phoneNumberBox.asWidget());
	}

	private void setContainersVisible() {
		this.phoneContainer.setVisible(!this.add);
		this.emailContainer.setVisible(!this.add);
	}
}
